package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.mapper.OpcFlowMessageGroupWayMapper;
import com.csun.cmny.provider.model.domain.OpcFlowMessageGroup;
import com.csun.cmny.provider.model.domain.OpcFlowMessageGroupWay;
import com.csun.cmny.provider.model.dto.message.FlowMessageGroupWayDto;
import com.csun.cmny.provider.model.vo.message.FlowMessageVo;
import com.csun.cmny.provider.model.vo.message.MessageWayVo;
import com.csun.cmny.provider.service.OpcFlowMessageGroupService;
import com.csun.cmny.provider.service.OpcFlowMessageGroupWayService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-07 11:06
 * @Description:
 */
@Service
@Transactional
public class OpcFlowMessageGroupWayServiceImpl extends BaseService<OpcFlowMessageGroupWay> implements OpcFlowMessageGroupWayService {

    @Resource
    private OpcFlowMessageGroupService opcFlowMessageGroupService;
    @Resource
    private OpcFlowMessageGroupWayMapper opcFlowMessageGroupWayMapper;

    /**
     * query group message way with code and group id
     *
     * @param code      the message code
     * @param groupId   the group id
     *
     * @return the flow message vo
     */
    @Override
    public FlowMessageVo queryGroupMessageWayWithCodeAndGroupId(String code, Long groupId) {

        return opcFlowMessageGroupWayMapper.queryGroupMessageWayWithCodeAndGroupId(code, groupId);
    }

    /**
     * bind group message way
     *
     * @param opcFlowMessageGroupWay
     * @return
     */
    @Override
    public Integer bind(OpcFlowMessageGroupWay opcFlowMessageGroupWay) {

        return opcFlowMessageGroupWayMapper.insertSelective(opcFlowMessageGroupWay);
    }

    /**
     * untied group message way
     *
     * @param opcFlowMessageGroupWay
     * @return
     */
    @Override
    public Integer untied(OpcFlowMessageGroupWay opcFlowMessageGroupWay) {

        return opcFlowMessageGroupWayMapper.delete(opcFlowMessageGroupWay);
    }

    /**
     * query group message way list
     *
     * @param opcFlowMessageGroupWay
     *
     * @return
     */
    @Override
    public List<MessageWayVo> queryWayList(OpcFlowMessageGroupWay opcFlowMessageGroupWay) {

        List<Long> wayIdList = opcFlowMessageGroupService.queryMessageWayIdList(opcFlowMessageGroupWay);

        FlowMessageGroupWayDto flowMessageGroupWayDto = new FlowMessageGroupWayDto();
        if (PublicUtil.isEmpty(wayIdList)) {
            return null; // 无法配置发送方式
        }

        flowMessageGroupWayDto.setGroupId(opcFlowMessageGroupWay.getGroupId());
        flowMessageGroupWayDto.setFlowMessageId(opcFlowMessageGroupWay.getFlowMessageId());
        flowMessageGroupWayDto.setWayIdList(wayIdList);

        return opcFlowMessageGroupWayMapper.queryWayList(flowMessageGroupWayDto);
    }
}
